<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>通过数据库升级流程</title>
  <link type="text/css" rel="stylesheet" th:href="@{${skinPath} + '/css.css'}" />
  <link href="index.css" rel="stylesheet" type="text/css">
  <link rel="stylesheet" th:href="@{/js/layui/css/layui.css}" media="all">
  <script th:src="@{/inc/common.js}"></script>
  <script th:src="@{/js/jquery-1.9.1.min.js}"></script>
  <script th:src="@{/js/jquery-migrate-1.2.1.min.js}"></script>
  <script th:src="@{/js/layui/layui.js}" charset="utf-8"></script>
  <link rel="stylesheet" th:href="@{/js/bootstrap/css/bootstrap.min.css}" />
  <script th:src="@{/js/bootstrap/js/bootstrap.min.js}"></script>
  <link th:href="@{/js/select2/select2.css}" rel="stylesheet"/>
  <script th:src="@{/js/select2/select2.js}"></script>
  <link th:href="@{/js/jquery-showLoading/showLoading.css}" rel="stylesheet" media="screen"/>
  <script type="text/javascript" th:src="@{/js/jquery-showLoading/jquery.showLoading.js}"></script>
  <script th:src="@{/js/jquery-alerts/jquery.alerts.js}" type="text/javascript"></script>
  <script th:src="@{/js/jquery-alerts/cws.alerts.js}" type="text/javascript"></script>
  <link th:href="@{/js/jquery-alerts/jquery.alerts.css}" rel="stylesheet" type="text/css" media="screen"/>
</head>
<body>
  <div style="text-align:center;margin:20px">
    <input type="button" value="上一步" class="btn btn-default" onclick="window.location.href='dataDict'"/>&nbsp;&nbsp;
    <strong>同步流程&nbsp;&nbsp;数据源</strong>
    <select id="dbSource" name="dbSource">
      <option value="">请选择</option>
      <option th:each="di : ${diInfo}"
              th:value="${di.name}"
              th:selected="${di.isDefault}"
              th:text="${di.name}">
      </option>
      <script th:inline="javascript">
        o('dbSource').value = [[${dbSource}]];
      </script>
    </select>
    &nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:;" onclick="if ('oa'==$('#dbSource').val()) {alert('不能修改默认的数据源oa'); return;} openWin('dbConnEdit?dbSource=' + $('#dbSource').val(), 640, 480)">修改数据源</a>
    <input type="button" class="btn btn-default" value="确定" onclick="window.location.href='upgradeFlowByDb?dbSource=' +  o('dbSource').value;"/>
    （注意本应用为旧版数据库，其它数据源为新版数据库）
  </div>
  <script th:inline="javascript">
    $('#dbSource').val('[(${dbSource})]');
  </script>
<th:block th:if="${!ifDbSource}">
  <div style="width: 95%; margin: 10px auto">
    <input type="button" class="btn btn-default" onclick="syncScriptsBatch()" value="批量同步脚本"/>
  </div>
  <table cellSpacing="0" cellPadding="3" width="95%" align="center" class="tabStyle_1">
    <tr>
      <td class="tabStyle_1_title" width="30">
        <input type="checkbox" id="all"/>
      </td>
      <td class="tabStyle_1_title" width="20%">编码</td>
      <td class="tabStyle_1_title" width="25%">新版</td>
      <td class="tabStyle_1_title" width="25%">本版</td>
      <td class="tabStyle_1_title" width="20%">操作</td>
    </tr>
    <tr th:each="ir : ${irInfo}">
      <td align="center">
        <th:block th:if="${ir.isFound}">
          <input class="chk" type="checkbox" th:value="${ir.codeNew}"/>
        </th:block>
      </td>
      <td th:text="${ir.codeNew}"></td>
      <td th:text="${ir.nameNew}"></td>
      <th:block th:if="${ir.isFound}">
        <td th:text="${ir.name}"></td>
      </th:block>
      <td>
        <th:block th:if="${ir.isFound}">
          <button class="btn btn-default" th:onclick="sync('[(${ir.codeNew})]', '[(${ir.nameNew})]')">同步流程</button>
          &nbsp;&nbsp;
          <button class="btn btn-default" th:onclick="syncScripts('[(${ir.codeNew})]', '[(${ir.nameNew})]')">同步脚本</button>
        </th:block>
      </td>
    </tr>
  </table>
</th:block>
</body>
<script>
  function sync(typeCode, name) {
    layer.confirm('您确定要同步"' + name + '"么？', {icon: 3, title: '提示'}, function (index) {
      layer.close();
      $.ajax({
        type: "post",
        url: "upgradeFlowByDbBtn",
        data: {
          op: "sync",
          typeCode: typeCode,
          dbSource: '[[${dbSource}]]'
        },
        dataType: "html",
        beforeSend: function (XMLHttpRequest) {
          $("body").showLoading();
        },
        success: function (data, status) {
          data = $.parseJSON(data);
          layer.msg(data.msg, {
            offset: '6px'
          });
        },
        complete: function (XMLHttpRequest, status) {
          $("body").hideLoading();
        },
        error: function (XMLHttpRequest, textStatus) {
          // 请求出错处理
          alert(XMLHttpRequest.responseText);
        }
      });
    });
  }

  function syncScripts(typeCode, name) {
      layer.confirm('您确定要同步"' + name + '"么？', {icon: 3, title: '提示'}, function (index) {
        layer.close();

        $.ajax({
          type: "post",
          url: "upgradeFlowByDbBtn",
          data: {
            op: "syncScripts",
            typeCode: typeCode,
            dbSource: '[[${dbSource}]]'
          },
          dataType: "html",
          beforeSend: function (XMLHttpRequest) {
            $("body").showLoading();
          },
          success: function (data, status) {
            data = $.parseJSON(data);
            layer.msg(data.msg, {
              offset: '6px'
            });
          },
          complete: function (XMLHttpRequest, status) {
            $("body").hideLoading();
          },
          error: function (XMLHttpRequest, textStatus) {
            // 请求出错处理
            alert(XMLHttpRequest.responseText);
          }
        });
      });
  }

  $(function() {
    $('#all').click(function() {
      $('.chk').prop('checked', $(this).prop('checked'));
    })
  })

  function syncScriptsBatch() {
    var codes = '';
    $('.chk').each(function() {
      if ($(this).prop('checked')) {
        if (codes == '') {
          codes = $(this).val();
        }
        else {
          codes += ',' + $(this).val();
        }
      }
    });
    if (codes == '') {
      layer.alert('请选择流程', '提示');
      return;
    }
    layer.confirm('您确定要批量同步脚本么？', {icon: 3, title: '提示'}, function (index) {
      layer.close();

      $.ajax({
        type: "post",
        url: "upgradeFlowByDbBtn",
        data: {
          op: "syncScriptsBatch",
          typeCodes: codes,
          dbSource: '[[${dbSource}]]'
        },
        dataType: "html",
        beforeSend: function (XMLHttpRequest) {
          $("body").showLoading();
        },
        success: function (data, status) {
          data = $.parseJSON(data);
          layer.msg(data.msg, {
            // window.location.reload();
          });
        },
        complete: function (XMLHttpRequest, status) {
          $("body").hideLoading();
        },
        error: function (XMLHttpRequest, textStatus) {
          // 请求出错处理
          alert(XMLHttpRequest.responseText);
        }
      });
    });
  }
</script>
</html>
